//
//  SECockpit.cpp
//  Space Explorer
//
//  Created by Yann Blaudin de Thé on 13/04/13.
//  Copyright (c) 2013 TeaTime. All rights reserved.
//

#include "SECockpit.h"


SECockpit::SECockpit(irr::IrrlichtDevice *device) {
    _guienv = device->getGUIEnvironment();
    irr::video::IVideoDriver* driver = device->getVideoDriver();
    
    irr::u32 screenW = driver->getScreenSize().Width;
    irr::u32 screenH = driver->getScreenSize().Height;
    
    irr::u32 margin = 250;
    irr::u32 space = 10;
    
    std::string font_path = GetApplicationResourcesPath() + "fontlucida.png";
    _font = _guienv->getFont(font_path.c_str());
    
    int font_height = _font->getDimension(L"").Height;
    
    date_time_txt = _guienv->addStaticText(L"",
                                           irr::core::rect< irr::s32 >(screenW-margin,0*font_height,
                                                                       screenW,1*font_height),
                                           true);
    date_time_txt->setDrawBorder(false);
    date_time_txt->setOverrideColor(irr::video::SColor(255, 255, 255, 0));
    date_time_txt->setOverrideFont(_font);
    
    mjd_txt = _guienv->addStaticText(L"",
                                           irr::core::rect< irr::s32 >(screenW-margin,1*font_height,
                                                                       screenW-margin/2,2*font_height),
                                           true);
    mjd_txt->setDrawBorder(false);
    mjd_txt->setOverrideColor(irr::video::SColor(255, 255, 255, 0));
    mjd_txt->setOverrideFont(_font);
    
    wrp_txt = _guienv->addStaticText(L"",
                                     irr::core::rect< irr::s32 >(screenW-margin/2+space,1*font_height,
                                                                 screenW,2*font_height),
                                     true);
    wrp_txt->setDrawBorder(false);
    wrp_txt->setOverrideColor(irr::video::SColor(255, 255, 255, 0));
    wrp_txt->setOverrideFont(_font);
    
    sim_time_txt = _guienv->addStaticText(L"",
                                     irr::core::rect< irr::s32 >(screenW-margin,2*font_height,
                                                                 screenW-margin/2,3*font_height),
                                     true);
    sim_time_txt->setDrawBorder(false);
    sim_time_txt->setOverrideColor(irr::video::SColor(255, 255, 255, 0));
    sim_time_txt->setOverrideFont(_font);
    
    fov_txt = _guienv->addStaticText(L"",
                                          irr::core::rect< irr::s32 >(screenW-margin/2+space,2*font_height,
                                                                      screenW,3*font_height),
                                          true);
    fov_txt->setDrawBorder(false);
    fov_txt->setOverrideColor(irr::video::SColor(255, 255, 255, 0));
    fov_txt->setOverrideFont(_font);
    
    fps_txt = _guienv->addStaticText(L"",
                                          irr::core::rect< irr::s32 >(screenW-margin,3*font_height,
                                                                      screenW-margin/2,4*font_height),
                                          true);
    fps_txt->setDrawBorder(false);
    fps_txt->setOverrideColor(irr::video::SColor(255, 255, 255, 0));
    fps_txt->setOverrideFont(_font);
    
    screen_txt = _guienv->addStaticText(L"",
                                     irr::core::rect< irr::s32 >(screenW-margin/2+space,3*font_height,
                                                                 screenW,4*font_height),
                                     true);
    screen_txt->setDrawBorder(false);
    screen_txt->setOverrideColor(irr::video::SColor(255, 255, 255, 0));
    screen_txt->setOverrideFont(_font);
    
    irr::f32 fov = device->getSceneManager()->getActiveCamera()->getFOV()*180.0f/M_PI;
    this->setFOV(fov);
    
    irr::core::stringw txt_str;
    txt_str = "[";
    txt_str += screenW;
    txt_str += "x";
    txt_str += screenH;
    txt_str += "x32]";
    screen_txt->setText(txt_str.c_str());
    
    _crosshair = new SECrosshair(_guienv);
    std::string viseur_path = GetApplicationResourcesPath() + "viseur.png";
    _crosshair->setTexture(driver->getTexture(viseur_path.c_str()));
    _crosshair->setSize(40);
    
}

SECockpit::~SECockpit() {
    delete _crosshair;
}

void SECockpit::setTime(SEClock &ck) {
    irr::core::stringw txt_str;
    
    txt_str = ck.getDateAndTimeStr();
    date_time_txt->setText(txt_str.c_str());
    
    char mjd_char[20];
    sprintf(mjd_char, "%.4f", ck.getSimMJD());
    
    txt_str = "MJD ";
    txt_str += mjd_char;
    mjd_txt->setText(txt_str.c_str());
    
    txt_str = "Sim ";
    txt_str += int(ck.getSimDuration());
    txt_str += "s";
    sim_time_txt->setText(txt_str.c_str());
    
}

void SECockpit::setFPS(int fps) {
    irr::core::stringw fps_str = "FPS ";
    fps_str += fps;
    
    fps_txt->setText(fps_str.c_str());
    
}

void SECockpit::setFOV(irr::f32 fov) {
    irr::core::stringw txt_str;
    txt_str = "FoV ";
    txt_str += int(fov);
    txt_str += L"°";
    fov_txt->setText(txt_str.c_str());
}

void SECockpit::setWarp(float warp) {
    irr::core::stringw wrp_str;
    
    if (warp == 1)
        wrp_str = "";
    else {
        int n_dig = int(round(log10(warp)));
        
        char warpfmt[5];
        if (n_dig < 0)
            sprintf(warpfmt, "%%.%.2if", -n_dig);
        else
            sprintf(warpfmt, "%%.0f");
        
        char warpchar[15];
        sprintf(warpchar, warpfmt, warp);
        
        wrp_str = "Wrp ";
        wrp_str += warpchar;
        wrp_str += "x";
    }
    
    wrp_txt->setText(wrp_str.c_str());
    
}


